<template>
  <div class="container">
    <el-card shadow="always" class="login-form">
      <h2 class="subtitle">系统登录</h2>
      <el-form
        ref="loginForm"
        :model="user"
        :rules="rules"
        status-icon
        label-width="50px"
      >
        <el-form-item label="账号" prop="account">
          <el-input v-model="user.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="passwd">
          <el-input v-model="user.passwd" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-upload"
            @click="login"
            size="small"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  methods: {
    //  登录
    login() {
      //使用elementui validate验证

      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          try {
            this.$axios.post(
              this.$api.user.login,
              this.user,
              res => {                
                if (res.data.isLogin) {
                  this.$store.commit("saveToken", res.data.token);
                  this.$store.commit(
                    "saveExpireTime",
                    new Date(res.data.expireTime)
                  );
                  this.$store.commit(
                    "saveExpireTimeOut",
                    new Date(res.data.expireTimeOut)
                  );
                  this.$router.push({path:'/'})
                } else {
                  this.$message({
                    type: "warning",
                    message: "登录失败",
                    duration: 1000 * 3
                  });
                }
              },
              err => {
                this.$message({
                  type: "warning",
                  message: "登录异常",
                  duration: 1000 * 3
                });
                this.$log.log(err);
              }
            );
          } catch (err) {
            this.$log.log(err);
          }
        } else {
          return false;
        }
      });
    }
  },
  data() {
    return {
      user: { account: "", passwd: "" }, //配合页面内的 prop 定义数据
      rules: {
        //配合页面内的 prop 定义规则
        account: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        passwd: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  mounted() {
    // console.log(this.$auth);
    // console.log(this.$auth.$state.loggedIn);
    // if (this.$auth.$state.loggedIn) this.$auth.logout();
  }
};
</script>

<style>
html {
  font-family: "Source Sans Pro", -apple-system, BlinkMacSystemFont, "Segoe UI",
    Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  word-spacing: 1px;
  -ms-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: border-box;
  margin: 0;
}

.button--green {
  display: inline-block;
  border-radius: 4px;
  border: 1px solid #3b8070;
  color: #3b8070;
  text-decoration: none;
  padding: 10px 30px;
}

.button--green:hover {
  color: #fff;
  background-color: #3b8070;
}

.button--grey {
  display: inline-block;
  border-radius: 4px;
  border: 1px solid #35495e;
  color: #35495e;
  text-decoration: none;
  padding: 10px 30px;
  margin-left: 15px;
}

.button--grey:hover {
  color: #fff;
  background-color: #35495e;
}

.container {
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.subtitle {
  font-weight: 300;
  color: #526488;
  word-spacing: 5px;
  padding-bottom: 15px;
  text-align: left;
}

.login-form {
  width: 400px;
  height: 270px;
}
</style>
